package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.BdActivity;
import com.example.demo.mapper.ActivityMapper;
import com.example.demo.utils.JDBCUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/activity")
public class ActivityController extends BaseController{

    @Resource
    ActivityMapper activityMapper;

    @PostMapping
    public Result<?> save(@RequestBody BdActivity BdActivity) {
        activityMapper.insert(BdActivity);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody BdActivity BdActivity) {
        activityMapper.updateById(BdActivity);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        activityMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(activityMapper.selectById(id));
    }

    @GetMapping("/synchronous")
    public Result<?> synchronous()  {
        try{
            JDBCUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("-1","同步失败");
        }
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String name,
                              @RequestParam(defaultValue = "") String isNeedBuild,
                              @RequestParam(defaultValue = "") String status,
                              @RequestParam(defaultValue = "") String startDate,
                              @RequestParam(defaultValue = "") String endDate) {
        LambdaQueryWrapper<BdActivity> wrapper = Wrappers.<BdActivity>lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(BdActivity::getName, name);
        }
        if (StringUtils.isNotBlank(isNeedBuild)) {
            wrapper.eq(BdActivity::getIsNeedBuild, isNeedBuild);
        }
        if (StrUtil.isNotBlank(status)) {
            wrapper.eq(BdActivity::getStatus, status);
        }
        if(StrUtil.isNotBlank(startDate )|| StrUtil.isNotBlank(endDate)){
            wrapper.and(q -> {
                if(StrUtil.isNotBlank(startDate)) {
                    q.ge(BdActivity::getStartDate, startDate);
                }
                if(StrUtil.isNotBlank(endDate)){
                    q.le(BdActivity::getEndDate,endDate);
                }
            });
        }
        wrapper.orderByDesc(BdActivity::getEndDate);
        Page<BdActivity> activityPage = activityMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(activityPage);
    }
    @GetMapping("/all")
    public Result<?> findAll() {
        LambdaQueryWrapper<BdActivity> wrapper = Wrappers.<BdActivity>lambdaQuery();
        wrapper.in(BdActivity::getIsNeedBuild,1);
        wrapper.eq(BdActivity::getStatus,"进行中");
        return Result.success(activityMapper.selectList(wrapper));
    }
}
